
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maHoaTiet = ISS.SESSION.getSession('session_maHoaTiet');

    var motif = {};
    motif.maHoaTiet = _maHoaTiet;
    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editProductMotif',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(motif),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });
}

function loadDataComplete(motif){
    ISS.DOM.setVal('#AdProductMotif_code', motif.maHoaTiet);
    ISS.DOM.setVal('#AdProductMotif_name', motif.tenHoaTiet);
}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#AdProductMotifEdit_Modified', 'click', function () {
        sendInfoEdit();
    });
}

function sendInfoEdit() {
    var motif = {};
    motif.maHoaTiet = ISS.DOM.getVal('#AdProductMotif_code');
    motif.tenHoaTiet = ISS.DOM.getVal('#AdProductMotif_name');

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerMotifEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(motif),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendInfoEditComplete(color) {
    if(color) {
        alert('Cập nhật thành công');
        ISS.WINDOW.location('/jsp/admin/ad-product-motif-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }
}
